if(SIMULATOR STREQUAL "verilator")

	# Find verilator
	execute_process(
		COMMAND
			bash -c
			"verilator -V|grep ROOT|grep verilator|awk '{print $3}'|xargs echo -n"
		OUTPUT_VARIABLE V_ROOT)

	if(V_ROOT)
		message(STATUS "Find verilator root: ${V_ROOT}")
	else()
		message(
			FATAL_ERROR
				"Cannot find verilator, please installl (Verilator 4.218 2022-08-31)")
	endif()

	# Add verilator include path for build
	include_directories(${V_ROOT}/include ${V_ROOT}/include/vltstd)

	# add VERILATOR flags for mcv outputs
	add_definitions(-DUSE_VERILATOR)

	if(TRACE STREQUAL "ON")
		message(STATUS "Tracing waves is ON")
		add_definitions(-DVL_TRACE)
	else()
		message(STATUS "Tracing waves is OFF")
	endif()

	if(WAVE_FORMAT STREQUAL "VCD")
		message(STATUS "Wave format is VCD")
	else()
		message(STATUS "Wave format is FST")
	endif()

	if(COVERAGE STREQUAL "ON")
		message(STATUS "Coverage is ON")
		add_definitions(-DVL_COVERAGE)
	else()
		message(STATUS "Coverage is OFF")
	endif()

	if(SIM_MULTI_THREAD STREQUAL "ON")
		message(STATUS "Multi-threading is ON")
		add_definitions(-DVL_THREADED)
	else()
		message(STATUS "Multi-threading is OFF")
	endif()

	# DEFINE mcv verilator gen function
	function(mcv_gen_verilator_dut UTFileName UTModuleName UTSourceModuleName
					 UTSourceFilePath)
		set(OLD_WAVE "none")
		if(EXISTS ${PROJECT_SOURCE_DIR}/dut/${UTFileName}_trace)
			# find if file Makefile has --trace-fst
			file(READ ${PROJECT_SOURCE_DIR}/dut/${UTFileName}_trace/Makefile
					 MAKEFILE_CONTENT)

			# VCD
			string(FIND ${MAKEFILE_CONTENT} "--trace" HAS_TRACE)
			if(HAS_TRACE GREATER -1)
				set(OLD_WAVE "VCD")
			endif()
			# FST
			string(FIND ${MAKEFILE_CONTENT} "--trace-fst" HAS_TRACE_FST)
			if(HAS_TRACE_FST GREATER -1)
				set(OLD_WAVE "FST")
			endif()
		endif()

		message(STATUS "OLD_WAVE: ${OLD_WAVE}" " WAVE_FORMAT: ${WAVE_FORMAT}")
		if(OLD_WAVE STREQUAL WAVE_FORMAT)
			message(STATUS "Skip build dut/${UTFileName} for HAS_TRACE")
			return()
		endif()

		execute_process(
			COMMAND rm -rf ${PROJECT_SOURCE_DIR}/dut/${UTFileName}
			COMMAND rm -rf ${PROJECT_SOURCE_DIR}/dut/${UTFileName}_trace)

		if(WAVE_FORMAT STREQUAL "FST")
			set(WAVE_NAME "${UTModuleName}.fst")
		elseif(WAVE_FORMAT STREQUAL "VCD")
			set(WAVE_NAME "${UTModuleName}.vcd")
		else()
			message(FATAL_ERROR "WAVE_FORMAT must be FST or VCD")
		endif()
		execute_process(
			WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/mcv
			COMMAND rm -rf ${PROJECT_SOURCE_DIR}/dut/${UTFileName}
			COMMAND
				./build/bin/mcv -f ${UTSourceFilePath} -s ./template/xdut/cpp -t
				${PROJECT_SOURCE_DIR}/dut/${UTFileName} -S ${UTSourceModuleName} -T
				${UTModuleName} --sim verilator
			COMMAND rm -rf ${PROJECT_SOURCE_DIR}/dut/${UTFileName}_trace
			COMMAND
				./build/bin/mcv -f ${UTSourceFilePath} -s ./template/xdut/cpp -t
				${PROJECT_SOURCE_DIR}/dut/${UTFileName}_trace -S ${UTSourceModuleName}
				-T ${UTModuleName} -w ${WAVE_NAME} --sim verilator)
		if(NOT EXISTS ${PROJECT_SOURCE_DIR}/dut/${UTFileName})
			message(FATAL_ERROR "Cannot generate ${UTFileName}")
		endif()

		execute_process(WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/dut/${UTFileName}
										COMMAND make)
		execute_process(
			WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/dut/${UTFileName}_trace
			COMMAND make)

	endfunction()

endif()
